package com.duqc.web.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.duqc.web.service.ExportPdfService;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName ExportPdfServiceImpl
 * @Description ExportPdfServiceImpl
 * @Author duqc
 * @Date 2023/5/31
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ExportPdfServiceImpl implements ExportPdfService {

    private static final String TITLE_NAME = "长沙市科技项目评审（评估）专家库专家信息表";

    @Override
    public void pdf(HttpServletResponse response) {
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            //设置pdf文档大小
            Document document = new Document(PageSize.A4, 50, 50, 80, 50);
            PdfWriter.getInstance(document, outputStream);
            //打开文档
            document.open();
            //设置标题
            //设置中文字体
            BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font = new Font(baseFont);
            //字体大小、加粗
            font.setSize(14);
            font.setStyle(Font.BOLD);
            //设置段落信息
            Paragraph paragraph = new Paragraph(TITLE_NAME, font);
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.setLeading(5f);
            paragraph.setSpacingBefore(-12f);
            paragraph.setSpacingAfter(27f);
            //添加到文档中
            document.add(paragraph);

            //构建正文内容
            Object[][] data = setTableValue();
            //构建表格
            PdfPTable table = new PdfPTable(new float[]{120, 120, 120, 120});
            for (int i = 0; i < data.length; i++) {
                for (int j = 0; j < data[i].length; j++) {
                    PdfPCell pdfCell = new PdfPCell();
                    int[] merge = {1, 3, 6, 9, 10, 11, 13, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26};
                    //合并单元格
                    if (ArrayUtil.contains(merge, i) && j == 1) {
                        pdfCell.setRowspan(1);
                        pdfCell.setColspan(3);
                    }
                    setTableStyle(table, pdfCell);
                    //设置中文字体
                    BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
                            BaseFont.NOT_EMBEDDED);
                    Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
                    fontChinese.setColor(BaseColor.BLACK);
                    fontChinese.setSize(12);
                    if (j == 1 || j == 3) {
                        //字体加粗
                        fontChinese.setStyle(1);
                    }
                    //设置段落信息
                    Paragraph paragraph1 = new Paragraph(String.valueOf(data[i][j]), fontChinese);
                    pdfCell.setPhrase(paragraph1);
                    table.addCell(pdfCell);
                }
            }
            //添加到文档中
            document.add(table);
            //关闭文档
            document.close();

        } catch (Exception e) {
            log.info(e.getMessage(), e);
        }
    }

    /**
     * 设置表格样式
     *
     * @param table PdfPTable
     * @param cell  PdfPCell
     */
    @SneakyThrows
    public static void setTableStyle(PdfPTable table, PdfPCell cell) {
        // 设置表格样式
        table.setLockedWidth(true);
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.setTotalWidth(new float[]{90, 160, 90, 160});
        // 设置单元格样式
        cell.setMinimumHeight(35);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setPadding(3);
        cell.setNoWrap(false);
    }

    private Object[][] setTableValue() {
        return new Object[][]{{"姓名", "baseInfo.getExpertName()", "评审角色", "baseInfo.getReviewRoleName()"},
                {"专家类型", "baseInfo.getExpertTypeName()"},
                {"证件类型", "baseInfo.getCertificateTypeName()", "性别", "baseInfo.getExpertGenderName()"},
                {"证件编号", "baseInfo.getExpertNumber()"},
                {"出生年月", "baseInfo.getExpertBirthday()", "年龄", "baseInfo.getExpertAge()"},
                {"职称", "baseInfo.getExpertProfessionalName()", "职级", "baseInfo.getExpertRankName()"},
                {"其它资格或水平证书", "baseInfo.getOtherCertificate()"},
                {"职务", "baseInfo.getExpertPosition()", "移动电话", "baseInfo.getExpertPhone()"},
                {"电子邮箱", "baseInfo.getExpertEmail()", "民族", "baseInfo.getExpertNationalityName()"},
                {"个人主页", "baseInfo.getExpertWebsite()"},
                {"单位名称", "baseInfo.getExpertEnterprise()"},
                {"所在二级单位", "baseInfo.getExpertDepartment()"},
                {"单位类别", "baseInfo.getExpertTypeName()", "所在区域", "baseInfo.getExpertAreaName()"},
                {"单位地址", "baseInfo.getEnterpriseAddress()"},
                {"毕业院校", "baseInfo.getGraduateSchool()", "所学专业", "baseInfo.getMajorName()"},
                {"最高学历", "baseInfo.getHighestDegreeName()", "最高学位", "baseInfo.getHighestOfferingName()"},
                {"学科分类", "subjectInfo"},
                {"高新技术领域分类", "technicalInfo"},
                {"研究方向", "baseInfo.getResearchDirection()"},
                {"海外留学经历", "baseInfo.getStudyAbroadName()", "参与国际合作项目", "baseInfo.getInternationalProjectName()"},
                {"是否院士", "baseInfo.getIsAcademicianName()"},
                {"工作经历", "baseInfo.getWorkExperience()"},
                {"所获国家称号", "baseInfo.getNationalTitleName()"},
                {"所获省部级称号", "baseInfo.getProvincialTitleName()"},
                {"所获市级称号", "baseInfo.getMunicipalTitleName()"},
                {"只要业绩、科研成果、荣誉等", "baseInfo.getPerformanceInfo()"},
                {"主要评审经历", null},
                {"开户行", "baseInfo.getOpeningBank()"},
                {"开户人", "baseInfo.getOpeningAccount()", "银行账号", "baseInfo.getBankNumber()"}};
    }

}
